package cn.iocoder.yudao.module.trade.controller.admin.orderverifycode;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.trade.controller.admin.orderverifycode.vo.*;
import cn.iocoder.yudao.module.trade.dal.dataobject.orderverifycode.OrderVerifyCodeDO;
import cn.iocoder.yudao.module.trade.service.orderverifycode.OrderVerifyCodeService;

@Tag(name = "管理后台 - 订单核销码")
@RestController
@RequestMapping("/trade/order-verify-code")
@Validated
public class OrderVerifyCodeController {

    @Resource
    private OrderVerifyCodeService orderVerifyCodeService;

    @PostMapping("/create")
    @Operation(summary = "创建订单核销码")
    @PreAuthorize("@ss.hasPermission('trade:order-verify-code:create')")
    public CommonResult<Long> createOrderVerifyCode(@Valid @RequestBody OrderVerifyCodeSaveReqVO createReqVO) {
        return success(orderVerifyCodeService.createOrderVerifyCode(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新订单核销码")
    @PreAuthorize("@ss.hasPermission('trade:order-verify-code:update')")
    public CommonResult<Boolean> updateOrderVerifyCode(@Valid @RequestBody OrderVerifyCodeSaveReqVO updateReqVO) {
        orderVerifyCodeService.updateOrderVerifyCode(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除订单核销码")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('trade:order-verify-code:delete')")
    public CommonResult<Boolean> deleteOrderVerifyCode(@RequestParam("id") Long id) {
        orderVerifyCodeService.deleteOrderVerifyCode(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得订单核销码")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('trade:order-verify-code:query')")
    public CommonResult<OrderVerifyCodeRespVO> getOrderVerifyCode(@RequestParam("id") Long id) {
        OrderVerifyCodeDO orderVerifyCode = orderVerifyCodeService.getOrderVerifyCode(id);
        return success(BeanUtils.toBean(orderVerifyCode, OrderVerifyCodeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得订单核销码分页")
    @PreAuthorize("@ss.hasPermission('trade:order-verify-code:query')")
    public CommonResult<PageResult<OrderVerifyCodeRespVO>> getOrderVerifyCodePage(@Valid OrderVerifyCodePageReqVO pageReqVO) {
        PageResult<OrderVerifyCodeDO> pageResult = orderVerifyCodeService.getOrderVerifyCodePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, OrderVerifyCodeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出订单核销码 Excel")
    @PreAuthorize("@ss.hasPermission('trade:order-verify-code:export')")
    @OperateLog(type = EXPORT)
    public void exportOrderVerifyCodeExcel(@Valid OrderVerifyCodePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<OrderVerifyCodeDO> list = orderVerifyCodeService.getOrderVerifyCodePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "订单核销码.xls", "数据", OrderVerifyCodeRespVO.class,
                        BeanUtils.toBean(list, OrderVerifyCodeRespVO.class));
    }



}